package project.base.common.util

import org.springframework.http.HttpStatus

/**
 * 响应工具类
 * @author: 三味
 * @update: 2022/3/3 16:37
 */
object ResponseUtil {
    fun <T> success(status: HttpStatus, data: T): ResponseResult<T> {
        return of(true, status.reasonPhrase, status.value(), data, null)
    }

    fun <T> failed(status: HttpStatus, data: T): ResponseResult<T> {
        return of(false, status.reasonPhrase, status.value(), data, null)
    }

    fun <T> of(success: Boolean, message: String?, code: Int, data: T, error: Throwable?): ResponseResult<T> {
        return ResponseResult<T>(success, message, code, data, error)
    }

    fun <T> ok(): ResponseResult<T?> {
        return ok<T>(null)
    }

    fun <T> ok(data: T?): ResponseResult<T?> {
        val status = HttpStatus.OK
        return success(status, data)
    }

    fun <T> created(): ResponseResult<T?> {
        val status = HttpStatus.CREATED
        return success<T?>(status, null)
    }

    fun <T> accepted(): ResponseResult<T?> {
        val status = HttpStatus.ACCEPTED
        return success<T?>(status, null)
    }

    fun <T> deleted(): ResponseResult<T?> {
        return of<T?>(true, "DELETED", 204, null, null)
    }

    fun <T> badRequest(): ResponseResult<T?> {
        val status = HttpStatus.BAD_REQUEST
        return failed<T?>(status, null)
    }

    fun <T> unauthorized(): ResponseResult<T?> {
        val status = HttpStatus.UNAUTHORIZED
        return failed<T?>(status, null)
    }

    fun <T> forbidden(): ResponseResult<T?> {
        val status = HttpStatus.FORBIDDEN
        return failed<T?>(status, null)
    }

    fun <T> notfound(): ResponseResult<T?> {
        val status = HttpStatus.NOT_FOUND
        return failed<T?>(status, null)
    }

    fun <T> methodNotAllowed(): ResponseResult<T?> {
        val status = HttpStatus.METHOD_NOT_ALLOWED
        return failed<T?>(status, null)
    }

    fun <T> notAcceptable(): ResponseResult<T?> {
        val status = HttpStatus.NOT_ACCEPTABLE
        return failed<T?>(status, null)
    }

    fun <T> gone(): ResponseResult<T?> {
        val status = HttpStatus.GONE
        return failed<T?>(status, null)
    }

    fun <T> serverError(): ResponseResult<T?> {
        val status = HttpStatus.INTERNAL_SERVER_ERROR
        return failed<T?>(status, null)
    }

    fun <T> badGateway(): ResponseResult<T?> {
        val status = HttpStatus.BAD_GATEWAY
        return failed<T?>(status, null)
    }

    fun <T> serviceUnavailable(): ResponseResult<T?> {
        val status = HttpStatus.SERVICE_UNAVAILABLE
        return failed<T?>(status, null)
    }

    fun <T> gatewayTimeout(): ResponseResult<T?> {
        val status = HttpStatus.GATEWAY_TIMEOUT
        return failed<T?>(status, null)
    }
}
